/*
 * @FilePath: /tads/server/app/business/apis/tactics.go
 * @Author: wnina0x0
 * @Date: 2025-04-14 14:08:33
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
package apis

import (
	"fmt"
	"net/http"
	"tads/server/app/business/service"
	"tads/server/app/business/service/dto"
	"tads/server/core/sdk/api"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Tactics struct {
	api.Api
}

func (e Tactics) Inset(c *gin.Context) {
	req := dto.TacticsInsetRequests{}
	s := service.Tactics{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("请求参数错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	if req.TacticsName == "" || req.TacticsDesc == "" || req.TacticsDbusTypeText == "" || req.TacticsAttackMethodIdText == "" || req.TacticsTypeId == 0 || req.TacticsSceneId == 0 {
		e.Logger.Errorf("插入失败:%s\n", fmt.Errorf("缺少参数"))
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", fmt.Errorf("缺少参数"))})
		return
	}
	if err := s.Inset(&req); err != nil {
		e.Logger.Errorf("插入失败:%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	e.Logger.Info("场景数据插入成功")
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "场景数据插入成功",
	})
	return
}

func (e Tactics) GetPage(c *gin.Context) {
	req := dto.TacticsGetRequests{}
	s := service.Tactics{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.Query).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("请求参数错误: %s!\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}

	response, err := s.GetScene(&req)
	if err != nil {
		e.Logger.Errorf("查询失败:%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", err.Error())})
		return
	}
	e.Logger.Info("场景获取成功")
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "场景获取成功",
		"data": response,
	})
	return

}

func (e Tactics) Update(c *gin.Context) {
	req := dto.TacticsUpdateRequests{}
	s := service.Tactics{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("场景修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("场景修改失败：%s\n", err.Error())})
		return
	}
	if err = s.Update(&req); err != nil {
		e.Logger.Errorf("场景修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("场景修改失败：%s\n", err.Error())})
		return
	}
	e.Logger.Info("场景修改成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "场景修改成功"})
	return

}

func (e Tactics) Status(c *gin.Context) {
	req := dto.TacticsStatusRequests{}
	s := service.Tactics{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("场景状态修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("场景状态修改失败：%s\n", err.Error())})
		return
	}
	if err = s.Status(&req); err != nil {
		e.Logger.Errorf("场景状态修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("场景状态修改失败：%s\n", err.Error())})
		return
	}
	e.Logger.Info("场景状态修改成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "场景状态修改成功"})
	return

}
